package com.soft.base.util;
/**
 * class
 *
 * @author user
 * @date 2020/06/05
 */
public class WildcardUtil {
    private static char flag1 = '*';
    private static char flag2 = '?';

    public static boolean match(String pattern, String text) {
        return match(text, 0, pattern, 0);
    }

    private static boolean match(String s, int i, String p, int j) {
        if (i == s.length() && j == p.length()) {
            return true;
        }
        if (i == s.length() || j == p.length()) {
            return false;
        }

        if (p.charAt(j) == flag1) {
            while (j < p.length() && p.charAt(j) == flag1) {
                ++j;  //skip continuous '*'
            }
            if (j == p.length()) {
                return true;
            }
            while (i < s.length() && !match(s, i, p, j)) {
                ++i;
            }
            return i < s.length();
        } else if (p.charAt(j) == s.charAt(i) || p.charAt(j) == flag2) {
            return match(s, ++i, p, ++j);
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(match("a", "aa"));
        System.out.println(match("aa", "aa"));
        System.out.println(match("aa", "aaa"));
        System.out.println(match("*", "aa"));
        System.out.println(match("a*", "aa"));
        System.out.println(match("?*", "ab"));
        System.out.println(match("c*a*b", "aab"));
        System.out.println(match("中国", "中国"));
        System.out.println(match("中国*", "中国人"));
    }
}
